//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;

namespace EntLibContrib.Query.Configuration
{
    using Properties;

    /// <summary>
	/// Configuration data defining QueryDataBase. This configuration section defines the name and type
	/// of the IQuery class
	/// </summary>
	//[SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "DataBase")]
	public class QueryDataBase : NameTypeConfigurationElement
	{
		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="QueryDataBase"/> class.
		/// </summary>
        public QueryDataBase()
		{
		}

		/// <summary>
		/// Initialize a new instance of the <see cref="QueryDataBase"/> class with a name and the type of <see cref="IQuery"/>.
		/// </summary>
		/// <param name="name">The name of the configured <see cref="IQuery"/>.</param>
		/// <param name="type">The type of <see cref="IQuery"/>.</param>
        protected QueryDataBase(string name, Type type)
			: base(name, type)
		{
		}
		#endregion

		/// <summary>
		/// Get the set of <see cref="TypeRegistration"/> object needed to
		/// register the Query represented by this config element.
		/// </summary>
		/// <returns>The sequence of <see cref="TypeRegistration"/> objects.</returns>
		public virtual IEnumerable<TypeRegistration> GetRegistrations() //IConfigurationSource configurationSource)
		{
			var newExpression = GetQueryCreationExpression();

			var queryRegistration = new TypeRegistration<IQuery>(newExpression)
			{
				Name = this.Name,
				IsPublicName = true,
                Lifetime =TypeRegistrationLifetime.Transient 
			};

			return new[] { queryRegistration } ;//, GetInstrumentationProviderRegistration(configurationSource) };
		}

		/// <summary>
		/// Gets the creation expression used to produce a <see cref="TypeRegistration"/> during
		/// <see cref="GetRegistrations"/>.
		/// </summary>
		/// <remarks>
		/// This must be overridden by a subclass, but is not marked as abstract due to configuration serialization needs.
		/// </remarks>
		/// <returns>A <see cref="Expression"/> that creates a <see cref="IQuery"/></returns>
		protected virtual Expression<Func<IQuery>> GetQueryCreationExpression()
		{
            throw new NotImplementedException(InternalResources.MethodMustBeImplementedBySubclasses);
		}
	}
}